/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Modele.Connect;
import Modele.Dep;
import Modele.Med;
import Modele.Pays;
import Modele.Spe;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String reponse = null;
        if (request.getParameter("reponse") != null) {
            if ("nom".equals(request.getParameter("reponse"))) {
                String nom = request.getParameter("nom");
                if (nom == null) {
                    reponse = "nom.jsp";
                } else {
                    Collection<Med> lesNoms = new TreeSet<Med>();
                    Collection<Dep> lesDeps = p.getLesDeps();
                    for (Dep dep : lesDeps) {
                        Collection<Med> lesMeds = dep.getLesMeds();
                        for (Med med : lesMeds) {
                            if (med.getNom().startsWith(nom)) {
                                lesNoms.add(med);
                            }
                        }
                    }
                    request.setAttribute("nom", nom);
                    request.setAttribute("lesNoms", lesNoms);
                    reponse = "mednom.jsp";
                }
            } else if("departement".equals(request.getParameter("reponse"))) {
                String dep = request.getParameter("dep");
                if (dep == null) {
                    Collection<Dep> lesDeps = p.getLesDeps();
                    request.setAttribute("deps", lesDeps);
                    reponse = "departement.jsp";
                } else {
                    Collection<Med> lesMeds = p.getLeDep(dep).getLesMeds();
                    request.setAttribute("meds", lesMeds);
                    request.setAttribute("dep", p.getLeDep(dep));
                    reponse = "meddep.jsp";
                }
            } else  {
                 String spe = request.getParameter("spe"); 
                     if (spe == null) {
                    Collection<Spe> lesSpe = p.getLesSpe();
                    request.setAttribute("spes", lesSpe);
                    reponse = "spe.jsp";
                } else {
                    Collection<Spe> lesMeds = p.getLeSpe(spe).getLesMeds();
                    request.setAttribute("meds", lesMeds);
                    request.setAttribute("spe", p.getLeSpe(spe));
                    reponse = "medspe.jsp";
                    }
            }
        } else {
            reponse = "index.jsp";
        }
        request.getRequestDispatcher(reponse).forward(request, response);
    
    }  

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
